﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GBIT.Web;
using WebComponents;
using WebComponents.Data;
using System.Collections.Generic;

public partial class NhomSanPhamList : UserControlBase {
  protected void Page_Load(object sender, EventArgs e) {
    this.AddModule = "SanPham/ThemNhomSanPham.ascx";
    if (!Page.IsPostBack) {
      ShowCategory(CatetoryType.Product);
    }
    this.EditEnabled = true;
    this.DeleteEnabled = true;
    this.AddEnabled = true;
    this.Title = "Danh sách chuyên mục";
  }
  private void ShowCategory(CatetoryType type) {
    List<CategoryInfo> list = new List<CategoryInfo>();
    CategoryController.LoadCategory(ref list, type);
    rptCate.DataSource = list;
    rptCate.DataBind();
    if (list.Count == 0)
      this.DeleteEnabled = false;
    else
      this.DeleteEnabled = true;
  }
  protected void rptCate_ItemDataBound(object sender, RepeaterItemEventArgs e) {
    //CategoryInfo cate = e.Item.DataItem as CategoryInfo;
    //if (cate != null) {
    //  var list = CategoryController.GetByParentId((CatetoryType)cate.CateType, cate.CategoryID);
    //  Repeater rptChildCate = e.Item.FindControl("rptChildCate") as Repeater;
    //  rptChildCate.DataSource = list;
    //  rptChildCate.DataBind();
    //}
  }
  protected override void OnDeleteRecord() {
    foreach (RepeaterItem item in rptCate.Items) {
      HtmlInputCheckBox chk = item.FindControl("chkCate") as HtmlInputCheckBox;
      if (chk != null && chk.Checked) {
        CategoryController.Delete(ConvertType.ToInt(chk.Attributes["title"].ToString()));
      }
      Repeater rpt = item.FindControl("rptChildCate") as Repeater;
      if (rpt != null) {
        foreach (RepeaterItem i in rpt.Items) {
          HtmlInputCheckBox c = i.FindControl("chkCate") as HtmlInputCheckBox;
          if (c != null && c.Checked) {
            CategoryController.Delete(ConvertType.ToInt(c.Attributes["title"].ToString()));
          }
        }
      }
    }
    ShowCategory((CatetoryType)ConvertType.ToInt(cmbTheLoaiChuyenMuc.SelectedValue));
  }
  protected void cmbTheLoaiChuyenMuc_SelectedIndexChanged(object sender, EventArgs e) {
    ShowCategory((CatetoryType)ConvertType.ToInt(cmbTheLoaiChuyenMuc.SelectedValue));
  }
}
